//应付【审核/对账/结算单对账/管理】详情（发货单）

/**
* 主视图
*/
interface IFeeOrderDetailViewModel extends ng.IScope {
    /**
    * 标题
    */
    title: string;
    /**
    * 基本信息对象
    */
    OData: detailResponse;
    /**
    * 承运单数据对象
    */
    CData: carryOrderResponse;
    /**
    * 合同数据对象
    */
    ContractData: contract;
    /**
    * 总单详情对象
    */
    orderDetial: detailResponse;
    /**
    * 总单列表数据对象
    */
    orderTotalList: detailResponse[];
    /**
    * 费用列表数据对象
    */
    feeList: feeListResponse[];
    /**
    * 费用详情对象
    */
    costData: any;
    /**
    * 子单详情对象
    */
    orderChil: orderChildListResponse;
    /**
    * 子单详情对象2
    */
    orderChil_2: any;
    /**
    * 子单列表数据对象
    */
    orderChildrenList: orderChildListResponse[];
    /**
    * 回单状态
    */
    receiptStatus: string;
    /**
    * 回单签收日期
    */
    receiptTime: string;
    /**
    * 画布宽度
    */
    canvsaWidth: number;
    /**
    * 油卡状态
    */
    oilCardStatus: string;
    /**
    * 合同状态
    */
    contractStatus: string;
    /**
    * 承运单备注
    */
    carryOrderRemarks: string;
    /**
    * 预付款1
    */
    advancePriceOne: number;
    /**
    * 预付款2
    */
    advancePriceTwo: number;
    /**
    * 预付款1支付方式
    */
    advancePriceTypeOne: string;
    /**
    * 预付款2支付方式
    */
    advancePriceTypeTwo: string;
    /**
    * 运费总价(系统生成参考)
    */
    totalPrice: string;
    /**
    * 申请费用总和
    */
    totalFeePrice: string;
    /**
    * 审核通过显示隐藏
    */
    auditedBtnHide: boolean;
    /**
    * 退回办理显示隐藏
    */
    returnManageBtnHide: boolean;
    /**
    * 确定按钮显示隐藏
    */
    isCanChangeContractStatus: boolean;
    /**
    * 附件信息显示隐藏
    */
    isShowFJ: boolean;
    /**
    * 回单显示隐藏
    */
    isShowHD: boolean;
    /**
    * 备注禁用
    */
    isCanUpdateRemarks: boolean;
    /**
     * 紧急程度枚举
     */
    urgencyUnitList: ValueListItemResponse[];
    /**
     * 所需车型枚举
     */
    carTypeList: ValueListItemResponse[];
    /**
     * 所需车长枚举
     */
    carLengthList: ValueListItemResponse[];
    /**
     * 货物重量单位枚举
     */
    goodsNumUnitList: ValueListItemResponse[];
    /**
     * 价格单位枚举
     */
    receivablePriceUnitList: ValueListItemResponse[];
    /**
     * 结算方式枚举
     */
    settleTypeList: ValueListItemResponse[];
    /**
     * 回单状态枚举
     */
    receiptStatusList: ValueListItemResponse[];
    /**
     * 承运商类型枚举
     */
    carrierTypeList: ValueListItemResponse[];
    /**
     * 油卡状态枚举
     */
    oilCardStatusList: ValueListItemResponse[];
    /**
     * 合同状态枚举
     */
    contractStatusList: ValueListItemResponse[];
    /**
     * 是否含税
     */
    includeTaxList: ValueListItemResponse[];
    /**
     * 工程总量单位枚举
     */
    projectTotalUnitList: ValueListItemResponse[];
    /**
     * 付款方式枚举
     */
    paymentMethodList: ValueListItemResponse[];
    /**
     * 驾驶证上传组件
     */
    driverUploader: any;
    /**
     * 挂车证上传组件
     */
    trailerUploader: any;
    /**
     * 行驶证上传组件
     */
    travelUploader: any;
    /**
     * 身份证上传组件
     */
    idCardUploader: any;
    /**
     * 车辆照片上传组件
     */
    carPicUploader: any;
     /**
     * 道路运输证
     */
    roadTransportCardUploader:any
    
    /**
     * 道路运输经营许可证
     */
    roadTransportLicenseUploader:any;
    /**
     * 从业资格证
     */
    occupationalLicenseUploader:any;
    /**
     * 磅单照片
     */
    weightNoteUploader:any;
    /**
     * 回单上传组件
     */
    loaderInfoList: any;
    /**
    * 点击获取子单详情事件
    */
    EditChildren: (data: orderChildListResponse) => void;

    /**
    * 点击获取总单详情事件
    */
    getOrderDetial: (data: detailResponse) => void;

    /**
    * 点击获取费用详情事件
    */
    feeListDetail: (data: any) => void;
    /**
    * 审核通过事件
    */
    audited: () => void;
    /**
    * 退回办理事件
    */
    returnManage: () => void;
    /**
    * 确定事件
    */
    changeContractStatus: () => void;
    /**
    * 返回事件
    */
    back: () => void;

    /**
     * 点击图片时候触发，用以旋转放大图片
     */
    handleImgClick:(item:any)=>void;
    /**
     * 用以旋转放大的图片的路径
     */
    selectedImgSrc:string;

    attachList:OssUploaderItem[];

}


class FeeOrderDetailController {
    uploaderList: AddCarAttachmentDetail[];
    upfiles:AddCarAttachmentDetail[];
    baseImageUrl: string;
    /**
     * 费用编号(id)
     */
    id: string;
    /**
     * 订单编号(id)
     */
    orderId: string;
    /**
     * 承运单id
     */
    carrierOrderId: string;
    /**
     * 整单页面
     */
    name: string;
    /**
     * 合同编号(id)
     */
    contractId: string;



    constructor(private $scope: IFeeOrderDetailViewModel,
        private routeService: routeService,
        
        private feeService: IFeeService,
        private locationService: ILocationService,
        private valueService: IValueService,
        private orderService: IorderService,
        private goodsService: IGoodsService,
        private employeeService: IEmployeeService,
        private $state: angular.ui.IStateService,
        //   private areaApiService: IAreaService,
        private $location: ng.ILocationService,
        private $http: ng.IHttpService,
        private $ngBootbox: BootboxService,
        private commonService:ICommonService,
        private ossUploaderService:ossUploaderService
    ) {
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";
        this.uploaderList = [];
        this.init();
        this.upfiles=[];
    }


    init(): void {
        this.id = this.$location.search().feeId;
        this.orderId = this.$location.search().orderId;
        this.carrierOrderId = this.$location.search().carrierOrderId;
        this.name = this.$location.search().name;

        this.$scope.auditedBtnHide = true;
        this.$scope.returnManageBtnHide = true;
        this.$scope.isShowFJ = true;
        this.$scope.isShowHD = true;
        this.$scope.isCanChangeContractStatus = true;
        this.$scope.isCanUpdateRemarks = true;

        this.$scope.attachList=[
            {
                title:'驾驶证',
                uploader:null,
                uploaderName:'driverUploader'
            },{
                title:'挂车证',
                uploader:null,
                uploaderName:'trailerUploader'
            },{
                title:'行驶证',
                uploader:null,
                uploaderName:'travelUploader'
            },{
                title:'身份证',
                uploader:null,
                uploaderName:'idCardUploader'
            },{
                title:'车辆照片',
                uploader:null,
                uploaderName:'carPicUploader'
            },{
                title:'道路运输证',
                uploader:null,
                uploaderName:'roadTransportCardUploader'
            },{
                title:'道路运输经营许可证',
                uploader:null,
                uploaderName:'roadTransportLicenseUploader'
            },{
                title:'从业资格证',
                uploader:null,
                uploaderName:'occupationalLicenseUploader'
            },{
                title:'磅单照片',
                uploader:null,
                uploaderName:'weightNoteUploader'
            },
        ]
        const OssFile = this.ossUploaderService.getOssFile();
        this.$scope.attachList.forEach((attach,index)=>{
            attach.uploader=new OssFile(this.$scope,attach.uploader,{uploaderList:[],tag:index+1});
           
        })



        
        this.loadValueDropDown();

        //应付审核
        if (this.name == "Audit") {
            this.$scope.title = "应付审核（发货单)";
            this.feeService.feeDetail(this.id).then(res => {
                if (res.data.feeStatus !== "1") {
                    this.$scope.auditedBtnHide = false;
                    this.$scope.returnManageBtnHide = false;
                }
            }, rej => { })
        }
        //费用对账
        if (this.name == "Checking") {
            this.$scope.title = "费用对账（发货单）";
            this.$scope.isShowFJ = false;
            this.$scope.isCanChangeContractStatus = false;
            this.$scope.auditedBtnHide = false;
            this.$scope.returnManageBtnHide = false;
            this.$scope.isCanUpdateRemarks = false;
        }

        //结算单管理
        if (this.name == "SettleManage") {
            this.$scope.title = "费用对账（发货单）";
            this.$scope.isShowFJ = false;
            this.$scope.auditedBtnHide = false;
            this.$scope.returnManageBtnHide = false;
        }
        //应付管理
        if (this.name == "FeeManage") {
            this.$scope.canvsaWidth = 400;
            this.$scope.title = "查看详情（发货单）";
            this.$scope.auditedBtnHide = false;
            this.$scope.returnManageBtnHide = false;
        }
        //结算单管理详情
        if (this.name == "SettleManageDetail") {
            this.$scope.title = "费用对账（发货单）";
            this.$scope.isShowFJ = false;
            this.$scope.auditedBtnHide = false;
            this.$scope.returnManageBtnHide = false;
        }
        //付款结算单管理详情
        if (this.name == "SettlePayManageDetail") {
            this.$scope.title = "费用对账（发货单）";
            this.$scope.isShowFJ = false;
            this.$scope.auditedBtnHide = false;
            this.$scope.returnManageBtnHide = false;
        }

        //获取总单视图及详情
        this.orderService.detail(this.orderId).then((res) => {
            this.$scope.OData = res.data;
            this.$scope.orderTotalList = [res.data];
            this.$scope.getOrderDetial = this.getOrderDetial;
        }, (rej) => { })

        //回单状态
        this.feeService.receiptStatus(this.orderId).then((res) => {
            this.$scope.receiptStatus = res.data.receiptStatus;//回单状态
            this.$scope.receiptTime = res.data.receiptTime;//回单签收时间
        }, (rej) => { })

        //费用列表
        this.orderService.feeList(this.carrierOrderId).then((res) => {
            let feeListDate = [];
            this.$scope.totalFeePrice ;
            let totalFeePrice=0;
            if (this.name == "Audit" || this.name == "Checking" || this.name == "FeeManage" || this.name == "SettleManage" || this.name == "SettleManageDetail" || this.name == "SettlePayManageDetail") {
                for (var i = 0; i < res.data.data.length; i++) {
                    if (res.data.data[i].feeId == this.id) {
                        feeListDate.push(res.data.data[i]);
                    }
                    totalFeePrice = totalFeePrice + res.data.data[i].applyAmount;
                }
            } else {
                feeListDate = res.data.data;
            }
            this.$scope.totalFeePrice =totalFeePrice.toFixed(2);
            this.$scope.feeList = feeListDate;
            this.$scope.feeListDetail = this.feeListDetail;
        }, (rej) => { });


        //承运单信息
        this.orderService.carryOrder(this.carrierOrderId).then((res) => {
            this.contractId = res.data.contract.contractId;
            this.$scope.CData = res.data;
            this.$scope.ContractData = res.data.contract;
            this.$scope.oilCardStatus = res.data.contract.oilCardStatus;
            this.$scope.contractStatus = res.data.contractStatus;

            this.$scope.carryOrderRemarks = res.data.carryOrderRemarks;

            // this.$scope.advancePaymentList = res.data.advancePaymentList;
            this.$scope.advancePriceOne = null;
            this.$scope.advancePriceTypeOne = "";
            this.$scope.advancePriceTwo = null;
            this.$scope.advancePriceTypeTwo = "";

            if (res.data.advancePaymentList.length > 0) {
                this.$scope.advancePriceOne = res.data.advancePaymentList[0].advancePrice
                this.$scope.advancePriceTypeOne = res.data.advancePaymentList[0].advancePriceType
            }
            if (res.data.advancePaymentList.length > 1) {
                this.$scope.advancePriceTwo = res.data.advancePaymentList[1].advancePrice
                this.$scope.advancePriceTypeTwo = res.data.advancePaymentList[1].advancePriceType
            }

            //运费总价
            this.$scope.totalPrice = res.data.contract.totalPrice.toFixed(2);
        

            var attachments = res.data.attachment;
            if (attachments.length > 0) {
                attachments.forEach((item, index) => {
                    var tag = item[3];

                    this.$scope.attachList[Number(tag)-1].uploader.addByAttach(item)


                     this.uploaderList.push(item);
                })
            }

        }, (rej) => { })


        //获取子单视图及详情及回单附件
        this.orderService.orderChildList(this.orderId).then((res) => {
            this.$scope.orderChildrenList = res.data.data;
            this.$scope.EditChildren = this.EditChildren;


            //回单附件
            let orderChilderInfoList = res.data.data;
            this.$scope.loaderInfoList = [];
            const OssFile=this.ossUploaderService.getOssFile();

            
            for (let i = 0; i < orderChilderInfoList.length; i++) {
                let childerId = orderChilderInfoList[i].id;


                this.$scope.loaderInfoList.push({
                    title:'回单'+(i+1),
                    uploader:null,
                    uploaderName:'receipt'+(i+1)+'Uploader',
                    id:childerId
                })

               
            }
            
            this.$scope.loaderInfoList.forEach((itemL, index) => {
                let childerIdTwo = itemL.id;
                itemL.uploader=new  OssFile(this.$scope,itemL.uploader,{uploaderList:[],tag:index+1,multiple:true});
                let loaderNameTwo = "loader" + index;
                this.feeService.childOrderAttachment(itemL.id).then((res) => {
                    var newData = res.data['data'];
                    if (newData.length > 0) {
                        for (let i = 0; i < newData.length; i++) {
                            var attachment = []
                            let item = newData[i];
                            let obj = { attachmentId: item.id, attachmentName: item.name, attachmentPath: item.path,attachmentTag:null };
                            attachment.push(obj);
                            this.upfiles.push(obj);

                            let imgUrl = this.baseImageUrl + newData[i].id;
                            let fileName = item.name;
                            let itemid = item.id;
                            let fileId = item.path
                            // this.commonService.showFileItem(obj,this.$scope[loaderNameTwo])
                            itemL.uploader.addByAttach(obj)
                          

                        }
                    }

                }, (rej) => { })
            })

        }, (rej) => { })


        this.$scope.audited = this.audited;
        this.$scope.returnManage = this.returnManage;
        this.$scope.back = this.back;
        this.$scope.changeContractStatus = this.changeContractStatus;

        this.$scope.handleImgClick=(item)=>{this.$scope.selectedImgSrc=item.imgSrc;}
    }




    /**
    *审核通过事件 
    */
    audited = () => {
        this.$ngBootbox.confirm("确定审核通过吗？").then((res) => {
            this.feeService.auditedFee(this.id).then((res) => {
                this.$state.go("app.fee.feeAuditList", {name:"feeAuditList"});
            }, rej => { })
        }, (rel) => { })
    }
    /**
    *退回办理
    */
    returnManage = () => {
        this.$ngBootbox.confirm('确定退回办理吗?').then((res) => {
            this.feeService.returnManage(this.id).then((res) => {
                this.$state.go("app.fee.feeAuditList", {name:"feeAuditList"});
            }, (rej) => { })
        }, (rej) => { return; });
    }
    /**
    *返回
    */
    back = () => {
        //应付审核
        if (this.name == "Audit") {
            this.$state.go("app.fee.feeAuditList", {name:'feeAuditList'})
        }
        //费用对账
        if (this.name == "Checking") {
            this.$state.go("app.fee.feeCheckList", {name:'feeCheckList'})
        }
        //结算单管理
        if (this.name == "SettleManage") {
            this.feeService.feeDetail(this.id).then((res) => {
                this.feeService.getFeeSettleBillList("", true, res.data.feeSettleBillCode, "", "", "", "", "", "", "", "", "0,1,2,3,4,5,6,7,8,9,10,11", "", "",false, 0, -1).then((res) => {
                    var feeSettleList = res.data.data;
                    this.$state.go("app.fee.fee_settle_audit", { settleId: feeSettleList[0].id, name: "audit" })
                }, (rej) => { });
            }, (rej) => { })
        }
        //应付管理
        if (this.name == "FeeManage") {
            this.$state.go("app.fee.feeManagent", {name:'feeManagent'})
        }
        //结算单管理详情
        if (this.name == "SettleManageDetail") {
            this.feeService.feeDetail(this.id).then((res) => {
                this.feeService.getFeeSettleBillList("", true, res.data.feeSettleBillCode, "", "", "", "", "", "", "", "", "0,1,2,3,4,5,6,7,8,9,10,11", "", "",false, 0, -1).then((res) => {
                    var feeSettleList = res.data.data;
                    this.$state.go("app.fee.fee_settle_detail", { settleId: feeSettleList[0].id, name: 'settle' })
                }, (rej) => { });
            }, (rej) => { })
        }
        //付款管理详情
        if (this.name == "SettlePayManageDetail") {
            this.feeService.feeDetail(this.id).then((res) => {
                this.feeService.getFeeSettleBillList("", true, res.data.feeSettleBillCode, "", "", "", "", "", "", "", "", "0,1,2,3,4,5,6,7,8,9,10,11", "", "",false, 0, -1).then((res) => {
                    var feeSettleList = res.data.data;
                    this.$state.go("app.fee.fee_settle_detail", { settleId: feeSettleList[0].id, name: 'settlepay' })
                }, (rej) => { });
            }, (rej) => { })
        }
    }

    /**
    *修改合同状态
    */
    changeContractStatus = () => {
        this.$ngBootbox.confirm('确定修改合同状态吗?').then(() => {
            if (this.$scope.isCanUpdateRemarks == false) {
                this.feeService.changeContractStatus(this.contractId, this.$scope.oilCardStatus, this.$scope.contractStatus).then((res) => {
                    this.feeService.carrierOrderRemarks(this.carrierOrderId, this.$scope.carryOrderRemarks).then((res) => {
                        this.$state.go("app.fee.feeCheckList", {name:'feeCheckList'})
                    }, (rej) => { })
                }, (rej) => { })

            } else {
                this.feeService.changeContractStatus(this.contractId, this.$scope.oilCardStatus, this.$scope.contractStatus).then(res => {
                    this.$state.go("app.fee.feeCheckList", {name:'feeCheckList'})
                }, rej => { })

            }
        }, () => { return })
    }

    /**
    * 点击获取总单详情事件
    */
    getOrderDetial = (data: detailResponse) => {
        this.$scope.orderDetial = data;
    }

    /**
    * 点击获取子单详情事件
    */
    EditChildren = (data: any) => {
        this.$scope.orderChil = data;
        this.orderService.orderChild(data.id).then((res) => {
            this.$scope.orderChil_2 = res.data;
        }, (rel) => {
        })
    }

    /**
    * 点击获取费用详情事件
    */
    feeListDetail = (data: any) => {
        this.$scope.costData = data;
    }

    /**
     * 所有可枚举下拉
     */
    loadValueDropDown = () => {
        this.$scope.paymentMethodList = this.valueService.getPaymentMethodList().data;
        this.$scope.urgencyUnitList = this.valueService.getUrgencyUnitList().data;
        this.$scope.carTypeList = this.valueService.getCarTypeList().data;
        this.$scope.carLengthList = this.valueService.getCarLengthList().data;
        this.$scope.goodsNumUnitList = this.valueService.getGoodQuantityUnitList().data;
        var priceUnitList = this.valueService.getPriceUnitList().data;
        priceUnitList.splice(priceUnitList.length-2,1);
        this.$scope.receivablePriceUnitList = priceUnitList;
        this.$scope.settleTypeList = this.valueService.getSettlementTypeList().data;
        this.$scope.receiptStatusList = this.valueService.getReceiptStatusList().data;
        this.$scope.carrierTypeList = this.valueService.getCarrierTypeList().data;
        this.$scope.oilCardStatusList = this.valueService.getOilCCardStatusList().data;
        this.$scope.contractStatusList = this.valueService.getContractStatusList().data;
        this.$scope.includeTaxList = this.valueService.getLinePriceTypeList().data;
        this.$scope.projectTotalUnitList = this.valueService.getGoodQuantityUnitList().data;
        this.$scope.paymentMethodList = this.valueService.getPaymentMethodList().data;
    }




    /**
     * 选择上传文件后
     */
    onSuccessItem = (fileName: string, filePath: string, tag: number) => {
        let attachment = {
            attachmentId: "",
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: tag.toString()
        };
        this.uploaderList.push(attachment);
    }

    /**
     * 删除上传的附件
     */
    onDelete = (item: any) => {
        $("input[type=file]").val("");
        var imgName = item._file.name;
        this.uploaderList.forEach((item, index) => {
            if (item.attachmentName == imgName) {
                this.uploaderList.splice(index, 1);
            }
        });
    }

    /**
     * 下载附件
     */
    // onDownload = (item: any) => {
    //     var imgName = item._file.name;
    //     // this.uploaderList.forEach((item, index) => {
    //     //     if (item.attachmentName == imgName) {
    //     //         window.location.href = this.baseImageUrl + item.attachmentId;
    //     //     }
    //     // });
    //     window.location.href = item.imgSrc
    // }

    /**
     * 过滤图片格式
     */
    // ownFilter(file: File): boolean {
    //     let self:any = this;
    //     return self.queue.length < 10;
    // }


    /**
     * 回单下载
     */
    // downLoad = (item: any) => {
    //     window.location.href = this.baseImageUrl + item.imgUrl;
    // }

}

angular.module("tms").controller("feeOrderDetailController", ["$scope", "routeService",
    "feeService", "locationService", "valueService",
    "orderService", "goodsService", "employeeService", "$state", "$location",
    "$http",
    //"areaApiService",
    "$ngBootbox","commonService","ossUploaderService", FeeOrderDetailController])